package com.huhu.sort;

public class SelectSort {
    /**
     *
     * @param arr
     * @param n  表示数组的长度
     */
    public static void selectSort(int arr[],int n){
        int i,j;
        for(i=0;i<n;i++){
            int index=i;
            for(j=i+1;j<n;j++){
                if(arr[index]>arr[j]){
                    index=j;
                }
            }
            if(index!=i){
                int tmp = arr[i];
                arr[i] = arr[index];
                arr[index] = tmp;
            }

        }


}

    public static void main(String[] args) {
        int i;
        int[] a = {5,6,5,4,3};

        System.out.printf("before sort:");
        for (i=0; i<a.length; i++) {
            System.out.printf("%d ", a[i]);
        }
        System.out.printf("\n");

        selectSort(a,a.length);

        System.out.printf("after  sort:");
        for (i=0; i<a.length; i++) {
            System.out.printf("%d ", a[i]);
        }
        System.out.printf("\n");
    }

}
